package com.xindz.hgq.app;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.blankj.utilcode.util.Utils;
import com.kingja.loadsir.core.LoadSir;
import com.lztx.shared.shared.util.loadsir.callback.EmptyCallback;
import com.lztx.shared.shared.util.loadsir.callback.ErrorCallback;
import com.lztx.shared.shared.util.loadsir.callback.LoadingCallback;
import com.lztx.shared.shared.util.loadsir.callback.TimeoutCallback;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.xindz.hgq.database.greendao.bean.DaoSession;
import com.xindz.hgq.utils.greendao.DbManager;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.app
 * @describe describe
 * @anthor XINDZ
 * @time 2018/1/2 11:30
 * @change
 * @chang time
 * @class describe
 */
public class App extends Application {
    public static App app;
    private DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
        this.app = this;
        init();
        //初始化GreenDAO 的管理类
        daoSession = DbManager.getInstance(this).getDaoSession();
        Realm.init(this);
        RealmConfiguration realmConfig = new RealmConfiguration.Builder()
                .name("lztx.realm")
                .build();
        Realm.setDefaultConfiguration(realmConfig);

    }

    private void init() {
        Utils.init(this);
        initLogger(true);
        initLoadSir();

    }

    /**
     * 初始化日志
     *
     * @param state
     */
    private void initLogger(final boolean state) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag("XINDZ").build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return state;
            }
        });
    }

    /**
     * 初始化加载反馈框架
     */
    private void initLoadSir() {
        LoadSir.beginBuilder()
                .addCallback(new ErrorCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new LoadingCallback())
                .addCallback(new TimeoutCallback())
                .setDefaultCallback(LoadingCallback.class)
                .commit();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }
}
